import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/a.dart';
import 'package:finance_app/src/common/app_colors.dart';
import 'package:finance_app/src/utils/cash_utils.dart';
import 'package:finance_app/src/widgets/form/form_edit_status_inherited_widget.dart';
import 'package:finance_app/src/widgets/wrapper/normal_form_wrapper.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_svg/flutter_svg.dart';

class AdjustNumberForm extends StatefulWidget {
  final String title;
  final String content;
  final String numberTitle;
  final ValueChanged<int> onNumberCallback;
  final int initialNumber;
  final int otherValue;

  const AdjustNumberForm(
      {Key key,
      this.title,
      this.content,
      this.otherValue,
      this.numberTitle,
      this.onNumberCallback,
      this.initialNumber})
      : super(key: key);

  @override
  _AdjustNumberFormState createState() => _AdjustNumberFormState();
}

class _AdjustNumberFormState extends State<AdjustNumberForm> {
  TextEditingController controller = TextEditingController(text: '1');

  void _onReduce() {
    if (!FormEditStatusInheritedWidget.ofEnable(context)) return;

    int integer = int.parse(controller.text, onError: (e) => 0);
    integer--;
    setTextControllerValue(integer);
  }

  void _onAdd() {
    if (!FormEditStatusInheritedWidget.ofEnable(context)) return;

    int integer = int.parse(controller.text, onError: (e) => 0);
    integer++;
    setTextControllerValue(integer);
  }

  void setTextControllerValue(int value) {
    String string = value.toString();
    controller.value = TextEditingValue(
      text: string,
      selection: TextSelection.collapsed(offset: string.length),
    );
  }

  @override
  void initState() {
    super.initState();
    if (widget.initialNumber != null) {
      controller = TextEditingController(text: widget.initialNumber.toString());
    }
    controller.addListener(() {
      int integer = int.parse(controller.text, onError: (e) => null);
      if (integer == null || integer < 0) {
        setTextControllerValue(0);
        return;
      }
      widget.onNumberCallback?.call(integer);
    });
  }

  @override
  void dispose() {
    super.dispose();
    controller.dispose();
  }

  @override
  void didUpdateWidget(covariant AdjustNumberForm oldWidget) {
    super.didUpdateWidget(oldWidget);
    if (oldWidget.otherValue != widget.otherValue) {
      setState(() {});
    }
  }

  @override
  Widget build(BuildContext context) {
    bool isEnable = FormEditStatusInheritedWidget.ofEnable(context);

    return NormalFormWrapper(
      topPadding: getDp(12),
      bottomPadding: getDp(12),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(
                widget.title,
                style: TextStyle(
                  color: aceBlackColor,
                  fontSize: getSp(16),
                  fontWeight: FontWeight.w700,
                  height: 16 / 14,
                ),
              ),
              SizedBox(
                height: getDp(6),
              ),
              Text.rich(TextSpan(children: [
                TextSpan(
                  text: widget.content,
                  style: TextStyle(
                    color: aceBlackColor,
                    fontSize: getSp(14),
                    fontWeight: FontWeight.w400,
                    height: 1,
                  ),
                ),
                if (widget.otherValue != 0)
                  TextSpan(
                    text:
                        ' ${CashUtils.operation(widget.otherValue)} ${CashUtils.ignoreCashAmount(widget.otherValue)}',
                    style: TextStyle(
                      color: isEnable ? aceBlueColor : aceBlackColor,
                      fontSize: getSp(14),
                      fontWeight: isEnable ? FontWeight.w400 : FontWeight.w700,
                      height: 1,
                    ),
                  )
              ], style: TextStyle(fontFamily: "DinRoundPro"))),
            ],
          ),
          Column(
            children: [
              Text(
                widget.numberTitle,
                style: TextStyle(
                  color: aceBlackColor,
                  fontSize: getSp(14),
                  fontWeight: FontWeight.w400,
                  height: 1,
                ),
              ),
              _buildNumberInput(),
            ],
          )
        ],
      ),
    );
  }

  _buildNumberInput() => Row(
        mainAxisSize: MainAxisSize.min,
        children: [
          PressedOpacityButton(
            onPressed: _onReduce,
            child: SvgPicture.asset(
              A.assetsImagesStyleMinus,
              width: getDp(24),
              height: getDp(24),
              color: FormEditStatusInheritedWidget.ofEnable(context)
                  ? null
                  : aceGrayColor,
            ),
          ),
          SizedBox(
            width: getDp(65),
            child: TextField(
              inputFormatters: [
                FilteringTextInputFormatter.digitsOnly,
              ],
              enabled: FormEditStatusInheritedWidget.ofEnable(context),
              scrollPadding: EdgeInsets.zero,
              decoration: InputDecoration(
                border: InputBorder.none,
                enabledBorder: InputBorder.none,
              ),
              controller: controller,
              style: TextStyle(
                color: FormEditStatusInheritedWidget.ofEnable(context)
                    ? aceBlueColor
                    : aceBlackColor,
                fontSize: getSp(16),
                fontWeight: FontWeight.w700,
              ),
              textAlign: TextAlign.center,
            ),
          ),
          PressedOpacityButton(
            onPressed: _onAdd,
            child: SvgPicture.asset(
              A.assetsImagesStylePlus,
              width: getDp(24),
              height: getDp(24),
              color: FormEditStatusInheritedWidget.ofEnable(context)
                  ? null
                  : aceGrayColor,
            ),
          ),
        ],
      );
}
